package edu.gatech.cs4400.spring2014.A8.view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import edu.gatech.cs4400.spring2014.A8.control.WaitedOn;
import edu.gatech.cs4400.spring2014.A8.model.DoctorData;

public class DoctorProfilePanel extends JPanel implements WaitedOn, ActionListener {
	public static enum Feedback{SAVE, CANCEL};
	private static final String[] specialties = {"Hair", "Knees"};
	private static final long serialVersionUID = 1L;
	private static final int NUM_AVAILS = 5;
	
	private boolean finished;
	private DoctorData data;
	private Feedback feedback;
	private HashMap<String, JTextField> name2field;
	private SpecialtyBox specialtyBox;
	private AvailabilitiesPanel availsPanel;

	public DoctorProfilePanel(DoctorData data, boolean forceSave) {
		this.data = data;
		finished = false;
		name2field = new HashMap<String, JTextField>();
		
		JPanel panel1 = new JPanel();
		JTextField field;
		panel1.setLayout(new GridLayout(DoctorData.FIELD_NAMES.length + 1, 2));
		for (String fieldName : DoctorData.FIELD_NAMES) {
			if (fieldName != DoctorData.SPECIALTY) {
				field = new JTextField(15);
				name2field.put(fieldName, field);
				panel1.add(new JLabel(fieldName));
				panel1.add(field);
			}
		}
		panel1.add(new JLabel(DoctorData.SPECIALTY));
		specialtyBox = new SpecialtyBox();
		panel1.add(specialtyBox);
		
		fillFields();
		
		availsPanel = new AvailabilitiesPanel();
		JPanel panel2 = availsPanel;
		
		JPanel panel3 = new JPanel();
		panel3.setLayout(new GridLayout(1,2));
		JButton saveButton = new JButton("Save");
		saveButton.addActionListener(this);
		panel3.add(saveButton);
		if (!forceSave) {
			JButton cancelButton = new JButton("Cancel");
			cancelButton.addActionListener(this);
			panel3.add(cancelButton);
		}
		
		
		this.add(panel1);
		this.add(panel2);
		this.add(panel3);
	}
	
	private void fillFields() {
		name2field.get(DoctorData.LICENSENO).setText(data.getLicenseNo());
		name2field.get(DoctorData.FIRSTNAME).setText(data.getFirstName());
		name2field.get(DoctorData.LASTNAME).setText(data.getLastName());
		name2field.get(DoctorData.WORKPHONE).setText(data.getWorkPhone());
		specialtyBox.setSelectedItem(data.getSpecialty());
		name2field.get(DoctorData.HOMEADDR).setText(data.getHomeAddress());
		if (data.getDateOfBirth() != null) {
			name2field.get(DoctorData.DOB).setText(data.getDateOfBirth().toString());
		}
		else {
			name2field.get(DoctorData.DOB).setText("");
		}
		name2field.get(DoctorData.ROOMNO).setText(data.getRoomNo()+"");
	}
	
	private void saveData() {
		
		availsPanel.saveData();
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		switch (arg0.getActionCommand()) {
		case "Save" :
			feedback = Feedback.SAVE;
			saveData();
			break;
		case "Cancel":
			feedback = Feedback.CANCEL;
			break;
		}
		finished = true;
		synchronized(this) {
			this.notify();
		}
	}
	
	@Override
	public boolean isDone() {
		return finished;
	}
	
	public Feedback getFeedback() {
		return feedback;
	}
	
	private class SpecialtyBox extends JComboBox<String> {
		private static final long serialVersionUID = 1L;

		public SpecialtyBox() {
			for (String specialty : specialties) {
				this.addItem(specialty);
			}
		}
	}
	
	private class AvailabilitiesPanel extends JPanel implements ActionListener  {
		private static final long serialVersionUID = 1L;
		
		private HashMap<JButton, Availability> button2avail;
		private ArrayList<Availability> avails;
		private JPanel center;
		
		public AvailabilitiesPanel() {
			
			this.setLayout(new BorderLayout());
			this.add(new JLabel("Availability"), BorderLayout.NORTH);
			JButton newAvail = new JButton("+");
			newAvail.addActionListener(this);
			this.add(newAvail, BorderLayout.SOUTH);
			center = new JPanel();
			this.add(center, BorderLayout.CENTER);
			avails = data.getAvailability();
			if (avails.size() == 0) {
				avails.add(new Availability());
			}

			center.setLayout(new GridLayout(Math.min(avails.size(), NUM_AVAILS), 1));
			button2avail = new HashMap<JButton, Availability>();
			int count = 0;
			for (Availability avail : avails) {
				center.add(newPanel(avail));
				count++;
				if (count > NUM_AVAILS) {
					break;
				}
			}
			
		}
		
		private JPanel newPanel(Availability avail) {
			JPanel line = new JPanel();
			line.add(avail.toPanel());
			JButton del = new JButton("X");
			button2avail.put(del, avail);
			del.addActionListener(this);
			line.add(del);
			return line;
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			if (arg0.getActionCommand().equals("+")) {
				avails.add(new Availability());
			}
			else {
				avails.remove(button2avail.get(arg0.getSource()));
			}
			remakeAvails();
		}
		
		private void remakeAvails() {
			for (Availability avail : avails) {
				avail.updateFromPanel();
			}
			
			center.removeAll();
			if (avails.size() == 0) {
				avails.add(new Availability());
			}
			center.setLayout(new GridLayout(Math.min(avails.size(), NUM_AVAILS), 1));
			button2avail = new HashMap<JButton, Availability>();
			int count = 0;
			for (Availability avail : avails) {
				center.add(newPanel(avail));
				count++;
				if (count> NUM_AVAILS) {
					break;
				}
			}
			center.revalidate();
		}
		
		public void saveData() {
			for (Availability avail : avails) {
				avail.updateFromPanel();
			}
			data.setAvailabilty(avails);
			
			data.setLicenseNo(name2field.get(DoctorData.LICENSENO).getText());
			data.setFirstName(name2field.get(DoctorData.FIRSTNAME).getText());
			data.setLastName(name2field.get(DoctorData.LASTNAME).getText());
			data.setWorkPhone(name2field.get(DoctorData.WORKPHONE).getText());
			data.setSpecialty((String)specialtyBox.getSelectedItem());
			data.setHomeAddress(name2field.get(DoctorData.HOMEADDR).getText());
			data.setDateOfBirth(name2field.get(DoctorData.DOB).getText());
			data.setRoomNo(name2field.get(DoctorData.ROOMNO).getText());
		}
		
	}
}
